﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRMAdapter;
using System.Net;
using System.Text;
using System.IO;
using System.Xml;


namespace CRMWeb.Pages.AM
{
    public partial class EzidebitTestPage : CRMWeb.Utilities.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //string digitalKey = WebConfigurationManager.AppSettings["EziDebitDigitalKey"].ToString();
            //CRMAdapter.PayByEzidebit.AddCustomers(digitalKey, "", "", txtLastName.Text, "", "", "", "", "", "", "", "", txtConStrtDate.Text, txtSmsPayRem.Text, txtSmsFailRem.Text, txtSmsExpCard.Text, "");
            //ClearAll();



            //PayByEzidebit1.AddCustomer();

            StringBuilder xml = new StringBuilder();
            xml.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:px=\"https://px.ezidebit.com.au/\">");
            xml.Append("<soapenv:Header/>");
            xml.Append("<soapenv:Body>");
            xml.Append("<px:TestFunction>");
            xml.Append("<px:ParameterToTest>The quick red fox jumps over the lazy brown dog.</px:ParameterToTest>");
            xml.Append("</px:TestFunction>");
            xml.Append("</soapenv:Body>");
            xml.Append("</soapenv:Envelope>");
            //AddCustomerRequest(xml.ToString());
            //return s;

            Execute();
            //HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.demo.ezidebit.com.au/v3-3/nonpci?wsdl");
            ////req.ContentType = "text/xml";
            //req.Method = "POST";
            //req.Headers.Add("SOAPAction", "http://tempuri.org/Register");
            //req.ContentType = "text/xml; charset=utf-8"; 
            //using (Stream stm = req.GetRequestStream())
            //{
            //    using (StreamWriter stmw = new StreamWriter(stm))
            //    {
            //        stmw.Write(xml.ToString());
            //    }
            //}

            //WebResponse response = req.GetResponse();
            //Stream responseStream = response.GetResponseStream();
            //return responseStream;

            //PostXml("https://api.demo.ezidebit.com.au/v3-3/nonpci?singleWsdl", xml.ToString());

            //HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.demo.ezidebit.com.au/v3-3/nonpci");
            //req.Headers.Add("SOAPAction", "\"http://tempuri.org/Register\"");
            //req.ContentType = "text/xml; charset=\"utf-8\"";
            //req.Accept = "text/xml";
            //req.Method = "POST";

            //using (Stream stm = req.GetRequestStream())
            //{
            //    using (StreamWriter stmw = new StreamWriter(stm))
            //    {
            //        stmw.Write(xml.ToString());
            //    }
            //}

            //HttpWebResponse response = (HttpWebResponse)req.GetResponse();

            //StreamReader responseStream = new StreamReader(response.GetResponseStream());
            ////lbl.Text = responseStream.ReadToEnd();
            //responseStream.Close();
        }

        public static void Execute()
        {
            HttpWebRequest request = CreateWebRequest();
            XmlDocument soapEnvelopeXml = new XmlDocument();
//            soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
//<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
//<soap:Body>
//    <HelloWorld3 xmlns=""http://tempuri.org/"">
//        <parameter1>test</parameter1>
//        <parameter2>23</parameter2>
//        <parameter3>test</parameter3>
//    </HelloWorld3>
//</soap:Body>
//</soap:Envelope>");
            soapEnvelopeXml.LoadXml(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:px=""https://px.ezidebit.com.au/"">
   <soapenv:Header/>
   <soapenv:Body>
      <px:TestFunction>
         <px:ParameterToTest>Test1</px:ParameterToTest>
      </px:TestFunction>
   </soapenv:Body>
</soapenv:Envelope>");

            using (Stream stream = request.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    string soapResult = rd.ReadToEnd();
                    Console.WriteLine(soapResult);
                }
            }
        }
        /// <summary>
        /// Create a soap webrequest to [Url]
        /// </summary>
        /// <returns></returns>
        public static HttpWebRequest CreateWebRequest()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://api.demo.ezidebit.com.au/v3-3/nonpci?wsdl");
            webRequest.Headers.Add(@"SOAP:Action");
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }


       

        //public HttpWebRequest CreateWebRequest(string ASMXURL)
        //{



        //    string soapAction = Strings.Chr(34) + "http://voa.n/Service/Inventory" + Strings.Chr(34);
        //    //get soapAction from WSDL, May need toadd one extra (chr(34) for Java service

        //    HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(ASMXURL);

        //    webRequest2.Headers.Add("SOAPAction", soapAction);
        //    webRequest2.Headers.Add("Accept-Encoding", "gzip,deflate");
        //    // got it from SOAPUI program (you can downloadit, add your xml to verify the request --DO this FIRST--
        //    webRequest2.ContentType = "text/xml;charset=UTF-8";
        //    webRequest2.Method = "POST";
        //    //Then just follow this link
        //    // Reference: http://www.roelvanlisdonk.nl/?p=1893  or you may  try Spces code above.
        //}

        //public void PostXml(string url, string xml)
        //{
        //    byte[] bytes = Encoding.UTF8.GetBytes(xml);
        //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        //    request.Method = "POST";
        //    request.ContentLength = bytes.Length;
        //    request.ContentType = "text/xml";

        //    using (Stream requestStream = request.GetRequestStream())
        //    {
        //        requestStream.Write(bytes, 0, bytes.Length);
        //    }

        //    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        //    {
        //        if (response.StatusCode != HttpStatusCode.OK)
        //        {
        //            string message = String.Format("POST failed with HTTP {0}",
        //                                           response.StatusCode);
        //            throw new ApplicationException(message);
        //        }
        //    }
        //}

        //Method to clear all fields
        public void ClearAll()
        {

            txtLastName.Text = "";
            txtConStrtDate.Text = "";
            txtSmsPayRem.Text = "";
            txtSmsFailRem.Text = "";
            txtSmsExpCard.Text = "";
        }
    }
}